// axios 使用示例
import { http } from '@/utils/request'
import { authAPI, caseAPI, lawyerAPI } from '@/api'
import { ENV_CONFIG, DEBUG } from '@/config/env'

// 示例1: 基础请求
export const basicRequestExample = async () => {
  try {
    // GET 请求
    const users = await http.get('/users', { page: 1, limit: 10 })
    console.log('用户列表:', users)
    
    // POST 请求
    const newUser = await http.post('/users', {
      name: '张三',
      email: 'zhangsan@example.com'
    })
    console.log('新用户:', newUser)
    
    // PUT 请求
    const updatedUser = await http.put('/users/1', {
      name: '李四'
    })
    console.log('更新用户:', updatedUser)
    
    // DELETE 请求
    await http.delete('/users/1')
    console.log('删除用户成功')
    
  } catch (error) {
    console.error('请求失败:', error)
  }
}

// 示例2: 使用 API 服务
export const apiServiceExample = async () => {
  try {
    // 用户登录
    const loginResult = await authAPI.login({
      username: 'test',
      password: '123456'
    })
    console.log('登录成功:', loginResult)
    
    // 获取案件列表
    const cases = await caseAPI.getCaseList({
      page: 1,
      limit: 20,
      status: 'pending'
    })
    console.log('案件列表:', cases)
    
    // 获取律师资料
    const profile = await lawyerAPI.getProfile()
    console.log('律师资料:', profile)
    
  } catch (error) {
    console.error('API 调用失败:', error)
  }
}

// 示例3: 文件上传
export const fileUploadExample = async (file) => {
  try {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('type', 'document')
    
    const result = await http.post('/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    
    console.log('文件上传成功:', result)
    return result.url
  } catch (error) {
    console.error('文件上传失败:', error)
    throw error
  }
}

// 示例4: 带参数的请求
export const parameterRequestExample = async () => {
  try {
    // 查询参数
    const searchResult = await http.get('/search', {
      keyword: '法律咨询',
      category: 'civil',
      date: '2024-01-01'
    })
    console.log('搜索结果:', searchResult)
    
    // 路径参数
    const caseDetail = await http.get(`/cases/${123}`)
    console.log('案件详情:', caseDetail)
    
  } catch (error) {
    console.error('参数请求失败:', error)
  }
}

// 示例5: 错误处理
export const errorHandlingExample = async () => {
  try {
    // 模拟一个会失败的请求
    await http.get('/non-existent-endpoint')
  } catch (error) {
    if (error.response) {
      // 服务器返回错误状态码
      console.log('错误状态码:', error.response.status)
      console.log('错误数据:', error.response.data)
    } else if (error.request) {
      // 请求发送失败
      console.log('网络错误:', error.request)
    } else {
      // 其他错误
      console.log('其他错误:', error.message)
    }
  }
}

// 示例6: 并发请求
export const concurrentRequestExample = async () => {
  try {
    const [users, cases, statistics] = await Promise.all([
      http.get('/users'),
      http.get('/cases'),
      http.get('/statistics')
    ])
    
    console.log('并发请求结果:', { users, cases, statistics })
  } catch (error) {
    console.error('并发请求失败:', error)
  }
}

// 示例7: 请求配置
export const requestConfigExample = async () => {
  try {
    // 自定义超时时间
    const response = await http.get('/slow-api', {}, {
      timeout: 30000
    })
    
    // 自定义请求头
    const response2 = await http.post('/api-with-custom-header', data, {
      headers: {
        'X-Custom-Header': 'custom-value'
      }
    })
    
  } catch (error) {
    console.error('配置请求失败:', error)
  }
}

// 示例8: 环境变量使用
export const envVariableExample = () => {
  console.log('当前API地址:', ENV_CONFIG.API_BASE_URL)
  console.log('调试模式:', DEBUG)
  
  // 根据环境显示不同信息
  if (DEBUG) {
    console.log('开发环境 - 启用详细日志')
  } else {
    console.log('生产环境 - 优化性能')
  }
}

// 导出所有示例
export const examples = {
  basicRequest: basicRequestExample,
  apiService: apiServiceExample,
  fileUpload: fileUploadExample,
  parameterRequest: parameterRequestExample,
  errorHandling: errorHandlingExample,
  concurrentRequest: concurrentRequestExample,
  requestConfig: requestConfigExample,
  envVariable: envVariableExample
} 